package com.example.yunhailutong.zhongtan.Adapter;

/**
 * Created by yunhailutong on 2018/7/10.
 */


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;

import com.example.yunhailutong.zhongtan.fragment.ChengShiFengCaiFragment;
import com.example.yunhailutong.zhongtan.fragment.DangJianFragment;
import com.example.yunhailutong.zhongtan.fragment.MainFragment;
import com.example.yunhailutong.zhongtan.fragment.MineFragment;

import java.util.ArrayList;

/**
 *
 */
public class ViewPageAdapter extends FragmentPagerAdapter {

    private ArrayList<Fragment> fragments = new ArrayList<>();
    private Fragment currentFragment;

    public ViewPageAdapter(FragmentManager fm) {
        super(fm);
        fragments.add(new MainFragment());
        fragments.add(new DangJianFragment());
        fragments.add(new ChengShiFengCaiFragment());
        fragments.add(new MineFragment());
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        if (getCurrentFragment() != object) {
            currentFragment = ((Fragment) object);
        }
        super.setPrimaryItem(container, position, object);
    }

    /**
     * Get the current fragment
     */
    public Fragment getCurrentFragment() {
        return currentFragment;
    }
}
